import {
 	getToken,
 	removeToken,
 	removeData
 } from '@/utils/auth' //获取token
 
 import { getUrl } from '@/utils/address.js' //获取token
 


 function request(obj) {
 	return new Promise((resolve, reject) => {
 		const baseUrl = getUrl()
 		// const baseUrl = 'http://172.20.202.235:1024/dev-api'
 		// const baseUrl = 'http://jincai.sintro.cn/prod-api'
 		var method = obj.method || "GET";
 		var url = baseUrl + obj.url || "";
 		var data = obj.params || obj.data || {};
 		let token = getToken();
 		var header = obj.header || {
 			'Content-Type': obj.contentType || 'application/json;charset=UTF-8',
 			'Authorization': 'Bearer ' + token
			// 'Authorization': 'Bearer ' + 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImI5MTVhM2MyLWMzYzktNDZmOS04MTZiLWQ0MmUyNGRmNWM5OCJ9.iLUHjbtKNUzUU_sPP0NZ54q4uhGuqKLncz8-xaTqi1TbXKxm8ayKYFoXG6lHsZ7-jI9LKml4LwkKeNVfEANzfw'
 		}
 		uni.request({
 			url: url,
 			data: data,
 			method: method,
 			header: header,
 			success: ((res) => {
 				if (res.statusCode === 403 || res.statusCode === 401 || res.data.code == 401) {
 					//清除用户信息
 					removeToken();
 					removeData();
 					uni.reLaunch({
 						// 错误处理，返回登录页
 						url: '/pages/login/login'
 					})

 				} else if (res.statusCode === 200) {
 					// console.log('request', res)
 					// uni.showToast({
 					//     title: '请求接口成功200',
 					//     duration: 2000
 					// })
 					resolve(res)
 				}
 			}),
 			fail: ((err) => {
 				// uni.showToast({
 				//     title: '服务正忙',
 				//     duration: 2000
 				// })
 				// uni.reLaunch({
 				// 	// 错误处理，返回维护页
 				//     url: '/pages/index/index1'
 				// })
 				reject(err)
 			})
 		})
 	})
 }
 export default request
 	
 
